//
//  DiaryViewController.m
//  四年
//
//  Created by 中软国际088 on 15/11/3.
//  Copyright © 2015年 小二. All rights reserved.
//

#import "DiaryViewController.h"
#import "AddMissionViewController.h"
#import "AddMemoryViewController.h"
#import "AddNavigationController.h"
#import "DiraySingle.h"
#import "IfCancelSingle.h"
#import "DirayDB.h"

@interface DiaryViewController ()
{
    UIView *sliderView;
    UISegmentedControl *mainSeg;
    NSMutableArray *missionArray;
    NSMutableArray *memoryArray;
    UITableView *table;
    
}
@property (nonatomic,strong)UIScrollView *scrollView;
@property (nonatomic,strong)UIView *leftView;
@property (nonatomic,strong)UIView *rightView;
@end

@implementation DiaryViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //设置背景颜色，防止卡顿
    self.view.backgroundColor =  [UIColor whiteColor];
    
    [self initarrays];
    
    [self initAddButton];
    
    [self initUISegmentedControl];
 
    [self initScrollView];
    
    [self initLeftView];
    
    [self initRightView];
    
    
    
    
    
}

//初始化两个装日记的数组
- (void)initarrays{
    missionArray = [[NSMutableArray alloc]init];
    
    memoryArray = [[NSMutableArray alloc]init];

    DirayDB * db = [[DirayDB alloc]init];
    
    NSMutableArray *arr = [db getAll];
    NSLog(@"%@",arr);
    
    for (Diray *d in arr) {
        if (d.context == nil) {
           //把context为空的日记对象放在倒计时日记数组中
           
            
            [missionArray addObject:d];
            
        }
        else
        {
            
            [memoryArray addObject:d];
            
        }
    }
    
    
}

//初始化导航栏添加按钮
-(void)initAddButton{
    
    UIBarButtonItem *addButton = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"add@2x.png"] style: UIBarButtonItemStylePlain target:self action:@selector(addEvent:)];
    
    
    
    self.navigationItem.rightBarButtonItem = addButton;
    

}

//右上角添加按钮触发事件
-(void)addEvent:(id)sender{
    
    //初始化单列日记
    [DiraySingle shareManager].DirayNew = [[Diray alloc]init];
    
    [IfCancelSingle shareManager].ifCancerNumber =0;
    
    AddNavigationController *addNavigation = [[AddNavigationController alloc]init];

    
    int a = (int) _scrollView.contentOffset.x;
   
  
    
    if (sender && [sender isKindOfClass:[UIBarButtonItem class]]) {
        if (a==0) {
            
            addNavigation.judgeNum = 0;
            
            AddMissionViewController *mission = [[AddMissionViewController alloc]init];
           
            [addNavigation addChildViewController:mission];
            
            
            [addNavigation initnavigationBarItems:mission];
            
            
            [self presentViewController:addNavigation animated:YES completion:nil];
            
        }
        if (a==WIDTH) {
            
            addNavigation.judgeNum = WIDTH;
            
            AddMemoryViewController *memory = [[AddMemoryViewController alloc]init];
            
             [addNavigation addChildViewController:memory];
            
            [addNavigation initnavigationBarItems:memory];
            
            [self presentViewController:addNavigation animated:YES completion:nil];
        }
    
    }

}

//初始化选择控件
- (void)initUISegmentedControl{
    
    //创建选择器
    NSArray *array = [[NSArray alloc]initWithObjects:@"生活录",@"倒计时", nil];
    
    mainSeg = [[UISegmentedControl alloc]initWithItems:array];
    
    mainSeg.frame = CGRectMake(-2, 64, WIDTH+4, 40);
    
    [mainSeg setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]} forState:UIControlStateNormal];
    [mainSeg setTitleTextAttributes:@{NSForegroundColorAttributeName:Main_Color} forState:UIControlStateSelected];
    
   mainSeg.backgroundColor = [UIColor colorWithRed:246/255.0 green:246/255.0 blue:246/255.0 alpha:1];
    
    mainSeg.tintColor = [UIColor clearColor];
    
    mainSeg.selectedSegmentIndex=0;
    
    
    [self.view addSubview:mainSeg];
    
    //创建滑动更随条
    sliderView = [[UIView alloc]initWithFrame:CGRectMake(0, 101, WIDTH/2, 3)];
    
    sliderView.backgroundColor = Main_Color;
    
    [self.view addSubview:sliderView];
    
    //segmengt点击事件
    
    [mainSeg addTarget:self action:@selector(valueChanged:) forControlEvents:UIControlEventValueChanged];

}

//UISegmentedControl点击事件
- (void)valueChanged:(UISegmentedControl *)seg{
  
    int a = (int) seg.selectedSegmentIndex;
    
    [_scrollView setContentOffset:CGPointMake(a * WIDTH, 0) animated:YES];
 
   
}

//初始化选择界面
- (void)initScrollView{
    
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 104, WIDTH, HEIGHT-104)];
    
    _scrollView.contentSize = CGSizeMake(2*WIDTH, HEIGHT-104);
    
    _scrollView.delegate = self;
    
    _scrollView.pagingEnabled = YES;
    
    //左边的view
    _leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-104-TabBar_Height)];
   
    [_scrollView addSubview:_leftView];
    
    //右边的view
    _rightView = [[UIView alloc]initWithFrame:CGRectMake(WIDTH, 0, WIDTH, HEIGHT-104-TabBar_Height)];
    
    [_scrollView addSubview:_rightView];
    
    [self.view addSubview:_scrollView];
    
}

//初始化左边视图
-(void)initLeftView{

//倒计时背景图片
    UIImageView *background = [[UIImageView alloc]initWithFrame:_leftView.frame];
    
    background.image = [UIImage imageNamed:@"memoryBackground.jpg"];
    
    [_leftView addSubview:background];
    
    //显示日记时间的表格
    
    table = [[UITableView alloc]initWithFrame:CGRectMake(0,104, WIDTH,missionArray.count*60) style:UITableViewStylePlain];
    
    table.alpha = 0.8;
    
    table.delegate = self;
    
    table.dataSource = self;
    
    [self.view addSubview:table];
    
    

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return missionArray.count;
    

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    static NSString *name =@"cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:name];
    
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:name];
        
        Diray *d = missionArray[indexPath.row];
        
        cell.textLabel.text = d.title;
    }
    return cell;
}

//获得编辑状态
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle==UITableViewCellEditingStyleDelete) {
        DirayDB *db = [[DirayDB alloc]init];
       Diray *d = missionArray[indexPath.row];
        
        //删除选中的日记事件
        [db del:d];
        
    
    }
    [self initarrays];
    
   table.frame =CGRectMake(0,104, WIDTH, missionArray.count*60);
    [table reloadData];


}


//滑动事件
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    if (scrollView && [scrollView isKindOfClass:[UIScrollView class]]) {
        
        //滑动的当前位置
        float location = scrollView.contentOffset.x;
        
      //  NSLog(@"%f",location);
        
        //让当前的表格左滑删除和滚动视图不发生冲突
        table.frame = CGRectMake(-location,104, WIDTH, missionArray.count*60);
        
        //当滑动到第一页时
        if (location == 0) {
            
            mainSeg.selectedSegmentIndex = 0;
            
            [mainSeg setTitleTextAttributes:@{NSForegroundColorAttributeName:Main_Color} forState:UIControlStateSelected];
             [mainSeg setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]} forState:UIControlStateNormal];
        }
        
        //当滑动到第二页时
        if (location == WIDTH) {
            
            mainSeg.selectedSegmentIndex = 1;
            
            [mainSeg setTitleTextAttributes:@{NSForegroundColorAttributeName:Main_Color} forState:UIControlStateSelected];
            [mainSeg setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor blackColor]} forState:UIControlStateNormal];
            
        }
        
    }
    

}

//重写生命周期viewWillAppear方法
- (void)viewWillAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    
    
    
    //如果日记单列有值代表新添加了日记并且判断是点击了确定按钮
    
    if ([IfCancelSingle shareManager].ifCancerNumber ==1 && ![[DiraySingle shareManager].DirayNew.title  isEqual: @""]) {
        
        //调用这本界面添加日记的方法
        [self showNewDiray];
        
    }
    [self initarrays];
    
    table.frame =CGRectMake(0,104, WIDTH, missionArray.count*60);
    
    [table reloadData];
    
    [super viewWillAppear:animated];
}

//显示新添加的日记
- (void)showNewDiray{
    
    Diray * diray = [DiraySingle shareManager].DirayNew;

    DirayDB *db = [[DirayDB alloc]init];
    
  
    [db add:diray];
    
   
    
   
    
}

//初始化右边视图
-(void)initRightView{
  
    //倒计时背景图片
    UIImageView *background = [[UIImageView alloc]initWithFrame:_leftView.frame];
    
    background.image = [UIImage imageNamed:@"memoryImage.jpg"];
    
    [_rightView addSubview:background];
    
    
}






//点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    if (tableView == table) {
        
        MissionDirayShowCtr *show = [[MissionDirayShowCtr alloc]init];
        
        Diray * d = missionArray[indexPath.row];
        
        show.diray =d;
        
        
        [self.navigationController pushViewController:show animated:YES];
        
    }
    


}

- (void)viewWillDisappear:(BOOL)animated{

    [super viewWillDisappear:animated];
    
   [DiraySingle shareManager].DirayNew = [[Diray alloc]init];

}
@end
